<?php


namespace app\api\logic;


use app\common\logic\BaseLogic;
use app\model\pms\Goods;
use app\model\pms\GoodsSku;
use app\model\user\Cart;
use support\utils\Time;
use think\facade\Db;

class CartLogic extends BaseLogic
{
    public function __construct()
    {
        $this->model = new Cart();
    }

    public function addCart($input)
    {
        $count = $this->model->where([
            'user_id' => request()->uid,
            'goods_id' => $input['goods_id'],
            'sku_id' => $input['sku_id'],
            'sku_name' => $input['sku_name'],
        ])->count();

        if ($count > 0) {
            $this->model->where([
                'user_id' => request()->uid,
                'goods_id' => $input['goods_id'],
                'sku_id' => $input['sku_id'],
                'sku_name' => $input['sku_name'],
            ])->update([
                "num" => $input['num'],
                "total_amount" => Db::raw("num * price"),
                "update_time" => Time::now(),
            ]);
        } else {
            $this->addCartData($input);
        }
    }

    public function editCart($input)
    {
        $count = $this->model->where([
            'user_id' => request()->uid,
            'goods_id' => $input['goods_id'],
            'sku_id' => $input['sku_id'],
            'sku_name' => $input['sku_name'],
        ])->count();

        if ($count > 0) {
            $this->model->where([
                'user_id' => request()->uid,
                'goods_id' => $input['goods_id'],
                'sku_id' => $input['sku_id'],
                'sku_name' => $input['sku_name'],
            ])->update([
                "num" => $input['num'],
                "total_amount" => Db::raw("num * price"),
                "update_time" => Time::now(),
            ]);
        } else {
            $this->addCartData($input);
        }
    }

    public function addCartData($input)
    {
        $goods = Goods::where([
            'id' => $input['goods_id']
        ])->field([
            'main_image',
            'name',
        ])->find();

        $sku = GoodsSku::where([
            'goods_id' => $input['goods_id'],
            'sku_name' => $input['sku_name'],
        ])->field([
            'original_price',
            'price',
        ])->find();
        $this->model->insert([
            'user_id' => request()->uid,
            'goods_id' => $input['goods_id'],
            'sku_id' => $input['sku_id'],
            'sku_name' => $input['sku_name'],

            "goods_name" => $goods['name'],
            "image" => $goods['main_image'],
            "original_price" => $sku['original_price'],
            "price" => $sku['price'],
            "num" => $input['num'],
            "total_amount" => $sku['price'] * $sku['num'],
            "create_time" => Time::now(),
            "update_time" => Time::now(),
        ]);
    }

    public function listCart()
    {
        $list = $this->model->where([
            'user_id' => request()->uid,
        ])->select()->toArray();
        if ($list) {
            $sku_ids = array_column($list, 'sku_id');
            $sku = GoodsSku::where([
                ['sku_id', 'in', $sku_ids,]
            ])->select()->toArray();

            $sku = array_column($sku, null, 'sku_id');
            $notNormal = [];
            foreach ($list as $key => $value) {
                if (!isset($sku[$value['sku_id']])) {
                    $notNormal[] = $value;
                    unset($list[$key]);
                    continue;
                }
                $curSku = $sku[$value['sku_id']];

                if ($curSku['stock'] <= 0 || $curSku['stock'] < $value['num']) {
                    $notNormal[] = $value;
                    unset($list[$key]);
                    continue;
                }
                if ($curSku['price'] != $value['price']) {
                    $notNormal[] = $value;
                    unset($list[$key]);
                    continue;
                }
                $list[$key]['stock'] = $curSku['stock'];
            }
            return [
                'list' => array_values($list),
                'notNormal' => $notNormal,
            ];
        }
        return [
            'list' => [],
            'notNormal' => [],
        ];
    }

    public function deleteCart()
    {
        $this->model->where([
            'user_id' => request()->uid,
        ])->delete();
    }

    public function cartCount()
    {
        return $this->model->where([
                'user_id' => request()->uid,
            ])->sum("num") ?? 0;
    }

    public function deleteSku($goods_id, $sku_id)
    {
        $this->model->where([
            'goods_id' => $goods_id,
            'sku_id' => $sku_id,
        ])->delete();
    }
}